home *** CD-ROM | disk | FTP | other *** search
/ Clickx 115 / Clickx 115.iso / software / tools / windows / tails-i386-0.16.iso / live / filesystem.squashfs / usr / lib / perl5 / DateTime / LeapSecond.pm < prev   
Encoding:
Perl POD Document  |  2010-07-30  |  4.2 KB  |  197 lines

  1. package DateTime::LeapSecond;
  2. BEGIN {
  3.   $DateTime::LeapSecond::VERSION = '0.61';
  4. }
  5.  
  6. use strict;
  7. use warnings;
  8.  
  9. use vars qw( @RD @LEAP_SECONDS %RD_LENGTH );
  10.  
  11. use DateTime;
  12.  
  13. # Generates a Perl binary decision tree
  14. sub _make_utx {
  15.     my ( $beg, $end, $tab, $op ) = @_;
  16.     my $step = int( ( $end - $beg ) / 2 );
  17.     my $tmp;
  18.     if ( $step <= 0 ) {
  19.         $tmp = "${tab}return $LEAP_SECONDS[$beg + 1];\n";
  20.         return $tmp;
  21.     }
  22.     $tmp = "${tab}if (\$val < " . $RD[ $beg + $step ] . ") {\n";
  23.     $tmp .= _make_utx( $beg,         $beg + $step, $tab . "    ", $op );
  24.     $tmp .= "${tab}}\n";
  25.     $tmp .= "${tab}else {\n";
  26.     $tmp .= _make_utx( $beg + $step, $end,         $tab . "    ", $op );
  27.     $tmp .= "${tab}}\n";
  28.     return $tmp;
  29. }
  30.  
  31. # Process BEGIN data and write binary tree decision table
  32. sub _init {
  33.     my $value = -1;
  34.     while (@_) {
  35.         my ( $year, $mon, $mday, $leap_seconds )
  36.             = ( shift, shift, shift, shift );
  37.  
  38.         # print "$year,$mon,$mday\n";
  39.  
  40.         my $utc_epoch
  41.             = DateTime->_ymd2rd( $year, ( $mon =~ /Jan/i ? 1 : 7 ), $mday );
  42.  
  43.         $value++;
  44.         push @LEAP_SECONDS, $value;
  45.         push @RD,           $utc_epoch;
  46.  
  47.         $RD_LENGTH{ $utc_epoch - 1 } = $leap_seconds;
  48.  
  49.         # warn "$year,$mon,$mday = $utc_epoch +$value";
  50.     }
  51.  
  52.     push @LEAP_SECONDS, ++$value;
  53.  
  54.     my $tmp;
  55.  
  56.     # write binary tree decision table
  57.  
  58.     $tmp = "sub leap_seconds {\n";
  59.     $tmp .= "    my \$val = shift;\n";
  60.     $tmp .= _make_utx( -1, 1 + $#RD, "    ", "+" );
  61.     $tmp .= "}\n";
  62.  
  63.     # NOTE: uncomment the line below to see the code:
  64.     #warn $tmp;
  65.  
  66.     eval $tmp;
  67.  
  68. }
  69.  
  70. sub extra_seconds {
  71.     exists $RD_LENGTH{ $_[0] } ? $RD_LENGTH{ $_[0] } : 0;
  72. }
  73.  
  74. sub day_length {
  75.     exists $RD_LENGTH{ $_[0] } ? 86400 + $RD_LENGTH{ $_[0] } : 86400;
  76. }
  77.  
  78. sub _initialize {
  79.  
  80.     # this table: ftp://62.161.69.5/pub/tai/publication/leaptab.txt
  81.     # known accurate until (at least): 2005-12-31
  82.     #
  83.     # There are no leap seconds before 1972, because that's the
  84.     # year this system was implemented.
  85.     #
  86.     # year month day number-of-leapseconds
  87.     #
  88.     _init(
  89.         qw(
  90.             1972  Jul. 1  +1
  91.             1973  Jan. 1  +1
  92.             1974  Jan. 1  +1
  93.             1975  Jan. 1  +1
  94.             1976  Jan. 1  +1
  95.             1977  Jan. 1  +1
  96.             1978  Jan. 1  +1
  97.             1979  Jan. 1  +1
  98.             1980  Jan. 1  +1
  99.             1981  Jul. 1  +1
  100.             1982  Jul. 1  +1
  101.             1983  Jul. 1  +1
  102.             1985  Jul. 1  +1
  103.             1988  Jan. 1  +1
  104.             1990  Jan. 1  +1
  105.             1991  Jan. 1  +1
  106.             1992  Jul. 1  +1
  107.             1993  Jul. 1  +1
  108.             1994  Jul. 1  +1
  109.             1996  Jan. 1  +1
  110.             1997  Jul. 1  +1
  111.             1999  Jan. 1  +1
  112.             2006  Jan. 1  +1
  113.             2009  Jan. 1  +1
  114.             )
  115.     );
  116. }
  117.  
  118. __PACKAGE__->_initialize();
  119.  
  120. 1;
  121. # ABSTRACT: leap seconds table and utilities
  122.  
  123.  
  124.  
  125. =pod
  126.  
  127. =head1 NAME
  128.  
  129. DateTime::LeapSecond - leap seconds table and utilities
  130.  
  131. =head1 VERSION
  132.  
  133. version 0.61
  134.  
  135. =head1 SYNOPSIS
  136.  
  137.   use DateTime;
  138.   use DateTime::LeapSecond;
  139.  
  140.   print "Leap seconds between years 1990 and 2000 are ";
  141.   print Date::Leapsecond::leap_seconds( $utc_rd_2000 ) -
  142.         Date::Leapsecond::leap_seconds( $utc_rd_1990 );
  143.  
  144. =head1 DESCRIPTION
  145.  
  146. This module is used to calculate leap seconds for a given Rata Die
  147. day.  It is used when DateTime.pm cannot compile the XS version of
  148. this code.
  149.  
  150. This library is known to be accurate for dates until December 2009.
  151.  
  152. There are no leap seconds before 1972, because that's the year this
  153. system was implemented.
  154.  
  155. =over 4
  156.  
  157. =item * leap_seconds( $rd )
  158.  
  159. Returns the number of accumulated leap seconds for a given day,
  160. in the range 0 .. 22.
  161.  
  162. =item * extra_seconds( $rd )
  163.  
  164. Returns the number of leap seconds for a given day,
  165. in the range -2 .. 2.
  166.  
  167. =item * day_length( $rd )
  168.  
  169. Returns the number of seconds for a given day,
  170. in the range 86398 .. 86402.
  171.  
  172. =back
  173.  
  174. =head1 SEE ALSO
  175.  
  176. E<lt>http://hpiers.obspm.fr/eop-pc/earthor/utc/leapsecond.htmlE<gt>
  177.  
  178. http://datetime.perl.org
  179.  
  180. =head1 AUTHOR
  181.  
  182. Dave Rolsky <autarch@urth.org>
  183.  
  184. =head1 COPYRIGHT AND LICENSE
  185.  
  186. This software is Copyright (c) 2010 by Dave Rolsky.
  187.  
  188. This is free software, licensed under:
  189.  
  190.   The Artistic License 2.0
  191.  
  192. =cut
  193.  
  194.  
  195. __END__
  196.  
  197.